package dmxGroupControl.listeners;

import dmxGroupControl.StatusCode;
import dmxGroupControl.StatusReporting;
import javafx.application.Platform;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import org.jdom2.Element;

import dmxGroupControl.LightController;
import dmxGroupControl.Universe;

public abstract class Listener implements StatusReporting
{
	boolean enabled = true;
	
	protected int fixtureStartIndex = 1;
	protected MappingCollection mappings;
	private String id;

    private Color statusColor = Color.BLACK;
    private String statusText = "Not started";
    private Rectangle statusRect = null;
    private Label statusLabel = null;

	public abstract void ParseMappings(Universe universe, int startIndex, Element xmlEntry);
	
	protected void ParseMappings(int startIndex)
	{
	    this.fixtureStartIndex = startIndex;
	}
	
	public abstract void close();

	protected Listener(String id)
	{
		this.mappings = new MappingCollection();
		this.id = id;
	}
	
	public void SetStartIndex(int index)
	{
	    this.fixtureStartIndex = index;
	}
	
	public boolean GetEnabled()
	{
		return enabled;
	}
	
	public boolean GetActive()
	{
		return enabled && LightController.AreUpdatesEnabled();
	}
	
	public void SetEnabled(boolean value)
	{
		this.enabled = value;
	}
	
	public static String[] GetSupportedTypes()
	{
		return new String[]
		{ "midi", "osc" };
	}

	public abstract String GetType();

	public abstract String GetAddress();

	public int GetStartIndex()
	{
		return fixtureStartIndex;
	}

	public MappingCollection GetMappings()
	{
		return mappings;
	}

	public void SetMappings(MappingCollection value)
	{
		this.mappings = value;
	}

    public String GetID()
    {
        return id;
    }

    @Override
    public Color GetStatusColor()
    {
        return statusColor;
    }

    @Override
    public String GetStatusText()
    {
        return statusText;
    }

    @Override
    public void SetStatusRect(Rectangle rectangle)
    {
        statusRect = rectangle;
        statusRect.setFill(statusColor);
    }

    @Override
    public void SetStatusLabel(Label label)
    {
        statusLabel = label;
        statusLabel.setText(statusText);
    }

    private void UpdateStatus(Color statusColor, String statusText)
    {
        this.statusColor = statusColor;
        this.statusText = statusText;
        if(statusRect != null)
        {
            Platform.runLater(
                () ->
                {
                    statusRect.setFill(statusColor);
                    statusLabel.setText(statusText);
                }
            );
        }
    }

    protected void UpdateStatus(StatusCode code)
    {
        UpdateStatus(code.getColor(), code.getCodeText());
    }

    protected void UpdateStatus(String error)
    {
        UpdateStatus(Color.RED, error);
    }

    protected void UpdateStatus(Exception e)
    {
        UpdateStatus(e.getMessage());
    }

    protected boolean AlreadyGreen()
    {
        return statusColor == Color.GREEN;
    }

    protected boolean AlreadyYellow()
    {
        return statusColor == Color.YELLOW;
    }
}
